package cn.shop.merchant.service;

import cn.hutool.core.util.StrUtil;
import cn.shop.merchant.mapper.player.PlayerAccountMapper;
import cn.shop.merchant.model.player.Player;
import cn.shop.merchant.model.player.PlayerAccount;
import cn.shop.merchant.req.PlayerQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import cn.shop.utils.enums.Status;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-3 13:10
 * @description : 玩家账号信息service
 */
@Slf4j
@Service
public class PlayerAccountService extends BaseServiceImpl<PlayerAccountMapper, PlayerAccount> {

	@Resource
	private RestTemplate restTemplate;
	@Resource
	private OrderService orderService;

	/**
	 * 获取玩家列表
	 *
	 * @param uid 商户编号
	 * @param req   查询条件
	 * @return 查询到的列表
	 */
	public Map<String, Object> list(PlayerQueryReq req, long uid) {
		Page<Player> page = Page.of(req.getCurrent(), req.getSize());
		page = baseMapper.playerList(page, uid, req);
		return PageUtil.formatPage(page, player -> format(player, uid));
	}

	/**
	 * 修改状态
	 *
	 * @param ids    玩家编号
	 * @param uid    商户编号
	 * @param status 状态
	 * @return 是否成功
	 */
	public boolean changeStatus(int[] ids, long uid, Status status) {
		PlayerAccount account = new PlayerAccount();
		account.setStatus(status);
		return update(account, Wrappers.<PlayerAccount>lambdaUpdate()
				.in(PlayerAccount::getId, Arrays.stream(ids).boxed().collect(Collectors.toList()))
				.eq(PlayerAccount::getUid, uid));
	}

	public boolean unbind(long uid, long bindId) {
		Player bind = baseMapper.getPlayer(uid, bindId);
		if (bind == null) {
			return true;
		}
		Map<String, Object> query = Putif.start()
				.anyway("partitionId", bind.getPartitionId())
				.anyway("account", bind.getGameAccount())
				.anyway("nickname", bind.getGameNickname())
				.anyway("code", "")
				.get();
		try {
			restTemplate.postForObject("http://localhost:83/game/api/bind/unbind", query, String.class);
		} catch (Exception ignored) {
			log.info("删除游戏服务器上绑定信息出错");
		}
		return SqlHelper.retBool(baseMapper.unbind(bindId, LocalDateTime.now()));
	}

	private Map<String, Object> format(Player player, Long uid) {
		BigDecimal sum = orderService.sumAmount(player.getPartitionId(), player.getGameNickname(), player.getGameAccount(), uid);
		return Putif.start()
				.anyway("bindId", player.getBindId())
				.anyway("accountName", player.getAccountName())
				.anyway("createTime", player.getCreateTime())
				.anyway("gameAccount", player.getGameAccount())
				.anyway("gameId", player.getGameId())
				.anyway("gameName", player.getGameName())
				.anyway("sumAmount", sum)
				.anyway("gameNickname", player.getGameNickname())
				.anyway("name", player.getName())
				.anyway("partitionName", player.getPartitionName())
				.get();

	}

}
